<template>
  <div>
    <el-form :model="model" :label-position="labelPosition" :rules="rules" ref="ruleForm" label-width="100px">
      <el-card>
        <div class="clearfix" slot="header">
          <span>订单信息</span>
          <div class="pull-right">
            <el-button type="primary" size="medium" :loading="loading" @click="submitForm($event, 'submit')">立即下单</el-button>
            <el-button type="primary" size="medium" :loading="loading" @click="submitForm($event, 'save')">保存至待转正</el-button>
          </div>
        </div>
        <el-row :gutter="15">
          <el-col :span="24">
            <el-form-item class="el-button-margin" label="业务类型:" prop="businessNo">
              <el-button size="medium"
                         :type="item.checked ? 'primary' : ''"
                         v-for="(item, index) in businessArrComputed"
                         :key="index" @click="checkBusiness(item)">{{item.text}}
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="服务类型:"
                          prop="serviceNo"
            >
              <el-button size="medium"
                         :type="item.checked ? 'primary' : ''"
                         v-for="(item, index) in serviceArrComputed"
                         :key="index" @click="checkservice(item)">{{item.text}}
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="下单方式:">
              <el-button :type="(model.orderWay == '1') ? 'primary' : ''" @click="checkDirectional(1)">抛出竞价</el-button>
              <el-button :type="(model.orderWay == '2') ? 'primary' : ''" @click="checkDirectional(2)">合作师傅定价</el-button>
            </el-form-item>
          </el-col>
          <template v-if="(model.orderWay == '2')">
            <el-col :lg="6" :md="12">
              <el-form-item label="服务方选择:" prop="wokerUserName" ref="wokerUserName">
                <el-input v-model="model.wokerUserName" :disabled="true">
                  <el-button slot="append" @click="checkWorker">选择</el-button>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="6" :md="12">
              <el-form-item label="服务电话:" prop="wokerPhone">
                <el-input v-model="model.wokerPhone" :disabled="true">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="6" :md="12">
              <el-form-item label="服务费:" prop="wokerPrice">
                <el-input v-model="model.wokerPrice">
                  <span slot="suffix">元</span>
                </el-input>
              </el-form-item>
            </el-col>
          </template>
        </el-row>
      </el-card>
      <el-card v-if="goodsConfig.modelCode">
        <div class="clearfix" slot="header">
          <span>{{goodsConfig.serviceInfo ? '服务信息' : '商品信息'}}</span>
          <el-button size="medium" class="pull-right" type="primary" plan @click="showModal()">添加产品</el-button>
        </div>
        <el-row :gutter="15">
          <el-col :span="24" v-if="!model.goods.length">
            <span style="color: #f56c6c;line-height: 50px;font-size: 16px;vertical-align: 30px">请添加产品信息！</span>
          </el-col>
          <el-col :lg="12" :md="24" v-for="(item, index) in model.goods" :key="index">
            <p class="shop-title">
              <span>{{index+1}}</span>
              <span class="pull-right">
                <i class="el-icon-delete text-cursor blue-color"
                   @click="delShopping(model.goods, index)"></i>
              </span>
            </p>
            <el-row :gutter="15">
              <el-col :lg="12" :md="24" v-if="!goodsConfig.class">
                <el-form-item label="类别:"
                              :prop="'goods.' + index + '.productCodeArr'"
                              :rules="{required: true, message: '请选择产品类型', trigger: 'change'}">
                  <product-select :key="item.id" ref="productSelect" v-model="item.productCodeArr"></product-select>
                </el-form-item>
              </el-col>
              <el-col :lg="12" :md="24" v-if="!goodsConfig.nums">
                <el-form-item label="数量:"
                              :prop="'goods.' + index + '.num'"
                              :rules="[{required: true, message: '请输入数量', trigger: 'blur'},
                              { validator: range, min: 1, max: 999, message: '请输入1~3位的整数', trigger: 'blur' },
                              { validator: isInteger, message: '请输入1~3位的整数', trigger: 'blur' }]">
                  <el-input :key="index" v-model="item.num">
                    <span slot="suffix">套</span>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item :label="goodsConfig.serviceInfo ? '描述:' : '商品名称:'"
                              :prop="'goods.' + index + '.pictureName'"
                              :rules="{required: true, max: 30, message: '产品名称不能超过30字', trigger: 'blur'}">
                  <el-input v-model="item.pictureName"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="选择图片:"
                              :prop="'goods.' + index + '.picture'"
                              :rules="{required: true, message: '请上传图片', trigger: 'change'}"
                >
                  <p>从 <span class="blue-color text-cursor" @click="uploadImg(item)">我的产品</span> 中选择
                    <span style="margin-left: 30px">(1~4张, <span :class="{textColor: item.picture.length >= 4}">{{item.picture.length}}/4</span>)</span>
                    <span style="margin-left: 30px;color: red" v-if="item.picture.length >= 4">提示：图片上传数量已达到限制</span>
                  </p>
                  <upload-image v-model="item.picture" @setFileId="setFileId" :formData="{group: id ? id : 'id'}"
                                :max="4"></upload-image>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-card>
      <el-card class="box-card" v-if="orderOver.modelCode">
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item :rules="allVolume" label="总体积:" prop="allVolume">
              <el-input v-model="model.allVolume">
                <span slot="suffix">m³</span>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="总重量:" prop="allWeight">
              <el-input v-model="model.allWeight">
                <span slot="suffix">KG</span>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item :rules="allPackages" label="总包件:" prop="allPackages">
              <el-input v-model="model.allPackages">
                <span slot="suffix">件</span>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
      <el-card v-if="consigneeInfo.modelCode || encity.modelCode">
        <div class="clearfix" slot="header">{{encity.modelCode ? '目的地信息' : '客户信息'}}</div>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="客户姓名:" prop="consigneeName">
              <el-input v-model="model.consigneeName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="客户电话:" prop="consigneePhone">
              <el-input v-model="model.consigneePhone"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="客户地址:" prop="address">
              <cascade-address :level="3" v-model="model.address" placeholder="请选择省市区"
                               style="width: 100%"></cascade-address>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="" label-width="0px" prop="consigneeAddress">
              <el-input v-model="model.consigneeAddress" placeholder="请输入详情地址"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="有无电梯:" prop="isElevator" class="noLabel">
              <el-radio-group v-model="model.isElevator">
                <el-radio label="1">有</el-radio>
                <el-radio label="0">无</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="楼层:" prop="floor">
              <el-input v-model="model.floor">
                <span slot="suffix">楼</span>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="预计服务:" prop="predictServiceDate">
              <el-date-picker
                type="date"
                format="yyyy-MM-dd"
                placeholder="预计服务时间"
                v-model="model.predictServiceDate" style="width: 100%;"
                :picker-options="pickerOptions1"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="服务时段:" prop="predictServiceDateCode">
              <el-select v-model="model.predictServiceDateCode" style="width: 100%;" placeholder="请选择服务时段">
                <el-option v-for="item of predictServiceDateCodeArr" :label="item.text" :value="item.value"
                           :key="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
      <el-card v-if="pickUpInfo.modelCode || becity.modelCode">
        <div class="clearfix" slot="header">
          <span>{{becity.modelCode ? '出发地信息' : '提货信息'}}</span>
          <el-button type="text" class="pull-right" size="mini" @click="choiceAddress">选择提货地址</el-button>
        </div>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="是否到货:" prop="isArriva" class="noLabel">
              <el-radio-group v-model="model.isArriva">
                <!--BSTE04货运搬家-->
                <el-radio :disabled="isScmOrder || model.businessNo == 'BSTE04'" :label="1">是</el-radio>
                <el-radio :disabled="isScmOrder || model.businessNo == 'BSTE04'" :label="2">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12" v-if="model.isArriva == 2">
            <el-form-item label="预计提货:" prop="predictDev提货信息liveryDate">
              <el-date-picker
                type="date"
                format="yyyy-MM-dd"
                placeholder="输入预计提货时间(选填)"
                v-model="model.predictDevliveryDate" style="width: 100%;"
                :picker-options="pickerOptions1"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <template v-if="model.isArriva == 1">
            <el-col :lg="6" :md="12">
              <el-form-item :key="model.isArriva" label="提货联系:" prop="deliveryName">
                <el-input v-model="model.deliveryName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="6" :md="12">
              <el-form-item :rules="deliveryPhoneRules" :key="model.isArriva" label="提货电话:" prop="deliveryPhone">
                <el-input v-model="model.deliveryPhone"></el-input>
              </el-form-item>
            </el-col>
          </template>
        </el-row>
        <el-row :gutter="15" v-if="model.isArriva == 1">
          <el-col :lg="6" :md="12">
            <el-form-item label="提货地址:" prop="DAddress">
              <cascade-address :level="3" v-model="model.DAddress" placeholder="请选择省市区"
                               style="width: 100%"></cascade-address>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="" prop="deliveryAddress" label-width="0">
              <el-input v-model="model.deliveryAddress" placeholder="请输入详情地址"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="12" :md="12">
            <el-form-item label="提货备注:" prop="deliveryMemo">
              <el-input v-model="model.deliveryMemo" placeholder="提货备注"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
      <el-card v-if="trunk.modelCode">
        <div class="clearfix" slot="header">
          <span>干线选择</span>
        </div>
        <el-row :gutter="15">
          <el-col :span="18">
            <el-table
              :data="computedTrunkData"
              border
              :header-cell-style="{'text-align': 'center'}"
              :cell-style="{'text-align': 'center'}"
              style="width: 100%; margin-top: 20px">
              <el-table-column
                prop="id"
                label="选择"
                width="60">
                <template slot-scope="scope">
                  <el-form-item label-width="0"
                  >
                    <el-checkbox v-model="scope.row.checked" @change="trunkChange(scope.row)"></el-checkbox>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                prop="startProvince"
                label="线路起点">
                <template slot-scope="scope">
                  <span>{{scope.row.startProvince}} {{scope.row.startCity}} {{scope.row.startDistrict}}</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="endProvince"
                label="线路终点">
                <template slot-scope="scope">
                  <span>{{scope.row.endProvince}} {{scope.row.endCity}} {{scope.row.endDistrict}}</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="aging"
                label="承诺时效">
              </el-table-column>
              <el-table-column
                prop="money"
                label="预估费用">
              </el-table-column>
              <el-table-column
                prop="server"
                label="服务方">
              </el-table-column>
              <el-table-column
                width="350"
                prop="compensate"
                label="赔付承诺">
              </el-table-column>
            </el-table>
          </el-col>
          <el-col :span="18">
            <pagination class="pull-right" :total-item="totalItem" @sizeChange="sizeChange" @pagination="pagination"
                        :per-item="params.pageSize"></pagination>
          </el-col>
        </el-row>
      </el-card>
      <el-card v-if="verifyInfo.modelCode">
        <div slot="header" class="clearfix">核销信息</div>
        <el-row :gutter="15">
          <el-col :span="24">
            <el-form-item label="核销类型:" prop="verfiyType">
              <el-select v-model="model.verfiyType" clearable placeholder="请选择核销类型">
                <el-option
                  v-for="item of verfiyTypeArr"
                  :label="item.text"
                  :value="item.value"
                  :key="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="model.verfiyType">
            <el-form-item label="核销单号:">
              <el-row :gutter="15">
                <el-col :span="4" style="margin-bottom: 20px;margin-left: -15px"
                        v-for="(item, index) in model.verifyOrderNoArr" :key="index">
                  <el-form-item
                    :prop="'verifyOrderNoArr.' + index + '.verifyOrderNo'"
                    :rules="{ required: true, message: '请填写核销单号', trigger: 'blur' }"
                    :key="index"
                  >
                    <el-input v-model="item.verifyOrderNo"
                              placeholder="请输入核销单号"
                    >
                      <i style="cursor: pointer"
                         class="el-icon-close"
                         slot="suffix"
                         @click="del(model.verifyOrderNoArr, index)">
                      </i>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-button type="primary" @click="add">添加</el-button>
                </el-col>
              </el-row>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
      <el-card v-if="boolCollectionInfo.modelCode">
        <div slot="header" class="clearfix">
          <span>代收款</span>
          <el-popover
            class="pull-right"
            placement="right"
            width="400"
            trigger="hover">
                    <span>
                      ①该单需要用户扫描支付成功后师傅方可签收；<br>
                      ②用户支付成功后代收金额进入钱包可以提现；<br>
                      ③每笔代收款有0.28%的手续费，由三方支付平
                        台双乾支付收取。如代收1000.00元，用户支
                        付1000.00元，钱包实际到账997.20元。
                    </span>
            <a slot="reference" href="javascript:;" class="green-color" style="font-size: 14px;text-decoration: none">代收款说明<i
              class="el-icon-question"></i></a>
          </el-popover>
        </div>
        <el-row :gutter="15">
          <el-table
            :data="model.collectionBeanList"
            border
            :summary-method="getSummaries"
            show-summary
            :header-cell-style="{'text-align': 'center'}"
            :cell-style="{'text-align': 'center'}"
            style="width: 100%; margin-top: 20px">
            <el-table-column
              prop="id"
              label="结算主体"
              align="center"
              width="180">
              <template slot-scope="scope">
                {{settlementObject}}
              </template>
            </el-table-column>
            <el-table-column
              prop="costNo"
              label="费用名称">
              <template slot-scope="scope">
                <el-form-item label-width="0" :prop="'collectionBeanList.' + scope.$index + '.costNo'">
                  <el-select v-model="scope.row.costNo" clearable>
                    <el-option :key="item.value" v-for="item in collectionBeanListAndDisable"
                               :disabled="item.disabled" :label="item.text" :value="item.value"></el-option>
                  </el-select>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              prop="money"
              align="center"
              label="费用金额">
              <template slot-scope="scope">
                <el-form-item label-width="0" :prop="'collectionBeanList.' + scope.$index + '.money'"
                              :rules="[
                              {validator: requiredIf, if: () => {return !!scope.row.costNo},   message: '应收金额必须是整数小于9位小数小于2位的小数', trigger: 'blur'},
                              {validator: isFormatNumber, int: 9,  message: '应收金额必须是整数小于9位小数小于2位的小数', trigger: 'blur'},
                              {validator: range, min: 0.01,  message: '应收金额必须大于0', trigger: 'blur'}
                              ]">
                  <el-input v-model="scope.row.money" clearable></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              prop="memo"
              label="代收备注">
              <template slot-scope="scope">
                <el-form-item label-width="0" :prop="'collectionBeanList.' + scope.$index + '.memo'"
                              :rules="[{max: 200, message: '允许输入200字内', trigger: 'blur'}]">
                  <el-input v-model="scope.row.memo"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              prop="amount3"
              label="操作">
              <template slot-scope="scope">
                <el-button size="small" type="danger" @click="handleDeleteCollectionBeanList(scope.$index)">删除</el-button>
                <el-button size="small" type="primary" @click.prevent="addCollectionBeanList">添加</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </el-card>
      <el-card>
        <div class="clearfix" slot="header">
          <span>其他信息</span>
          <span class="pull-right green-color">填写接单价后，在《我的毛利报表》中可以展示出相关的数据</span>
        </div>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="外部单号:" prop="orderSourceNo">
              <el-input v-model="model.orderSourceNo">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="接单价:" prop="wokerPickMoney">
              <el-input v-model="model.wokerPickMoney">
                <span slot="suffix">元</span>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="总货值:" prop="allCargoPrice">
              <el-input v-model="model.allCargoPrice">
                <span slot="suffix">元</span>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15" v-if="orderDetail.sourceWarehouse">
          <el-col :lg="6" :md="12">
            <el-form-item label="推单来源:">
              <span>{{orderDetail.sourceWarehouse}}</span>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="推单单号:">
              <span>{{orderDetail.outOrderNo}}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :lg="12" :md="12">
            <el-form-item label="备注:" prop="memo">
              <el-input type="textarea" :rows="5" v-model="model.memo" placeholder="允许输入200字内"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
      <el-card class="box-card">
        <el-row :gutter="15">
          <el-col :span="24" style="text-align: center">
            <el-button type="primary" size="medium" :loading="loading" @click="submitForm($event, 'submit')">立即下单
            </el-button>
            <el-button type="primary" size="medium" :loading="loading" @click="submitForm($event, 'save')">保存至待转正
            </el-button>
          </el-col>
        </el-row>
      </el-card>
    </el-form>
    <product-modal :visible.sync="visibleProduct" ref="productModal" @addcheckShopping="addcheckShopping" :editPicture="editPicture"></product-modal>
    <check-worker ref="CheckWorker" @editWorker="editWorker"></check-worker>
    <choice-address ref="ChoiceAddress"
                    :deliveryAddress="deliveryAddress"
                    :visible.sync="visibleAddress"
                    @handleDeliveryAddress="handleDeliveryAddress"
                    @getDeliveryAddress="getDeliveryAddress"
    ></choice-address>
  </div>
</template>
<script>
import ProductModal from '@/views/fhb/temporary/ProductModal'
import {getAppointServiceType, getSubDictListOfParentCode, getChCollectionCost} from '@/utils/selectOptions'
import {copy, alert, dotData, arrayColumn} from '@/utils'
import {filterDataBaseDictionary, decimalNumFilter} from '@/filters'
import {isFormatNumber, range, isInteger, isPhone, requiredIf} from '@/utils/validate'
import moment from 'moment'
import CheckWorker from '@/views/fhb/temporary/CheckWorker'
import ChoiceAddress from '@/views/fhb/temporary/ChoiceAddress'

export default {
  name: 'temporaryShowName',
  components: {
    ProductModal,
    CheckWorker,
    ChoiceAddress
  },
  filters: {
    filterDataBaseDictionary
  },
  data() {
    return {
      visibleProduct: false,
      visibleAddress: false,
      loading: false,
      httpLoading: false,
      isDirectional: true,
      productItem: '',
      editPicture: false,
      pickerOptions1: {
        disabledDate(time) {
          return time.getTime() < ((new Date()).getTime() - 3600 * 1000 * 24)
        }
      },
      id: this.$route.params.id,
      routeName: this.$route.name, // faTemporaryInput(录单界面)
      settlementObject: '',
      serviceArr: [],
      businessArr: [],
      params: {
        pageSize: 10,
        pageNum: 1
      },
      totalItem: 0,
      labelPosition: 'left', // label左对齐
      orderDetail: {},
      newPictureArr: [],
      model: {
        businessNo: 'BSTE02',
        serviceNo: [],
        orderWay: '1',
        wokerUserName: '',
        wokerPhone: '',
        wokerPrice: '',
        checked: '',
        verifyOrderNoArr: [{}],
        verfiyType: '',
        address: [],
        DAddress: [],
        goods: [],
        isElevator: '',
        predictServiceDate: '',
        predictServiceDateCode: '',
        predictDevliveryDate: '',
        memo: '',
        isArriva: '',
        consigneePhone: '',
        consigneeName: '',
        consigneeAddress: '',
        allVolume: '',
        allWeight: '',
        allPackages: '',
        allCargoPrice: '',
        deliveryName: '',
        deliveryPhone: '',
        deliveryMemo: '',
        collectionBeanList: [{
          costNo: '',
          memo: '',
          money: ''
        }],
        orderSourceNo: '',
        checkedTrunk: ''
      },
      collectionBeanList: [],
      deliveryAddress: [],
      verfiyTypeArr: [],
      predictServiceDateCodeArr: [],
      configureArr: [],
      trunkData: [],
      trunkMap: {},
      rules: {
        businessNo: [],
        serviceNo: [],
        wokerUserName: [
          {required: true, message: '请选择师傅', trigger: 'blur'}
        ],
        wokerPhone: [
          {required: true, message: '请选择师傅', trigger: 'blur'}
        ],
        wokerPickMoney: [
          {validator: isFormatNumber, int: 7, decimal: 2, message: '输入8位以下整数或2位小数', trigger: 'blur'}
        ],
        wokerPrice: [
          {required: true, message: '请输入定价价格', trigger: 'blur'},
          {validator: isFormatNumber, int: 7, decimal: 2, message: '输入8位以下整数或2位小数', trigger: 'blur'}
        ],
        allWeight: [
          {validator: isFormatNumber, int: 7, decimal: 2, message: '输入8位以下整数或2位小数1231', trigger: 'blur'}
        ],
        allCargoPrice: [
          {validator: isFormatNumber, int: 7, decimal: 2, message: '输入8位以下整数或2位小数', trigger: 'blur'}
        ],
        consigneeName: [
          {required: true, message: '请输入客户姓名', trigger: 'blur'}
        ],
        consigneePhone: [
          {required: true, message: '请输入客户电话', trigger: 'blur'},
          {validator: isPhone, message: '请输入正确的电话号', trigger: 'blur'}
        ],
        floor: [
          {validator: isInteger, message: '请输入正确的楼层', trigger: 'blur'},
          {validator: range, message: '请输入正确的楼层', min: -30, max: 400, trigger: 'blur'}
        ],
        serviceDate: [
          {type: 'date', required: true, message: '请选择日期', trigger: 'change'}
        ],
        numb: [
          {required: true, message: '请确定产品数量', trigger: 'blur'}
        ],
        address: [
          {required: true, message: '请选择客户地址', trigger: 'change'}
        ],
        consigneeAddress: [
          {required: true, max: 100, message: '允许输入100字内', trigger: 'blur'}
        ],
        isArriva: [
          {required: true, message: '请选择是否到货', trigger: 'change'}
        ],
        deliveryAddress: [
          {max: 100, message: '允许输入100字内', trigger: 'blur'}
        ],
        deliveryMemo: [
          {max: 200, message: '允许输入200字内', trigger: 'blur'}
        ],
        totalVolume: [
          {required: true, message: '必填', trigger: 'blur'}
        ],
        memo: [
          {max: 200, message: '允许输入200字内', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    // 获取商家信息
    this.$ajax.get('/ms-fahuobao-user/OutGoodsUser/findBusinessInfo').then(response => {
      if (response.success) {
        this.settlementObject = dotData(response.data, 'businessName')
      }
    })
    // 代收货款
    getChCollectionCost().then(arr => {
      if (Array.isArray(arr)) {
        this.collectionBeanList = arr
      }
    })
    getSubDictListOfParentCode('BSTE').then(data => {
      let arr = data || []
      arr.map(item => {
        item.checked = false
      })
      this.businessArr = data
    })
    getSubDictListOfParentCode('FHBHXLX').then(data => {
      let arr = []
      arr = copy(data, true)
      if (arr.length) {
        arr.push({value: '', text: '不选择核销类型'})
      }
      this.verfiyTypeArr = arr
    })
    getSubDictListOfParentCode('CH_TIME_INTERVAL').then(data => {
      this.predictServiceDateCodeArr = data
    })
    if (this.rotueName !== 'faTemporaryInput') { // 录单界面
      this.getOrderInfo()
    }
    this.getDeliveryAddress()
  },
  computed: {
    computedTrunkData() {
      if (this.model.trunkId && this.trunkData.length) {
        return this.trunkData.map(item => {
          if (item.id === this.model.trunkId) {
            this.$set(item, 'checked', true)
          }
          return item
        })
      } else {
        return this.trunkData
      }
    },
    businessArrComputed() {
      this.businessArr.forEach(item => {
        if (item.value === this.model.businessNo) {
          item.checked = true
        } else {
          item.checked = false
        }
      })
      return this.businessArr
    },
    serviceArrComputed() {
      let serviceArr = []
      this.serviceArr.forEach(item => {
        item.checked = false
        for (const v of this.model.serviceNo) {
          if (item.value === v) {
            item.checked = true
          }
        }
        serviceArr.push(item)
      })
      if (parseInt(this.model.orderWay) === 2) {
        return serviceArr.filter(v => v.value !== 'FHB16')
      }
      return serviceArr
    },
    deliveryPhoneRules() {
      let rules = [{required: true, message: '请输入电话号码', trigger: 'blur'}]
      return rules
    },
    isScmOrder() {
      let isScm = dotData(this.orderDetail, 'orderOrgType') === 'scm'
      if (isScm) {
        return true
      } else {
        return false
      }
    },
    allVolume() {
      let rules = [{validator: isFormatNumber, int: 7, decimal: 2, message: '输入8位以下整数或2位小数', trigger: 'blur'}]
      rules.push({required: true, message: '请输入总体积', trigger: 'blur'})
      return rules
    },
    allPackages() {
      let rules = [{validator: range, min: 1, max: 99999, message: '请输入1~5位的整数', trigger: 'blur'},
        {validator: isInteger, message: '请输入1~5位的整数', trigger: 'blur'}]
      rules.push({required: true, message: '请输入总包件', trigger: 'blur'})
      return rules
    },
    goodsConfig() {
      if (this.goods.modelCode) {
        let obj = copy(this.goods, true)
        obj.serviceInfo = false
        return obj
      } else if (this.serviceInfo.modelCode) {
        let obj = copy(this.serviceInfo, true)
        obj.serviceInfo = true
        return obj
      } else {
        return {}
      }
    },
    // 带有禁用的 费用选项
    collectionBeanListAndDisable() {
      let temp = arrayColumn(this.model.collectionBeanList, 'costNo')
      console.log('temptemp', temp)
      return this.collectionBeanList.map(v => {
        if (temp.indexOf(v.value) >= 0) {
          v.disabled = true
        } else {
          v.disabled = false
        }
        return v
      })
    },
    // serviceInfo--服务信息，goods--商品信息，orderOver--体积/重量/包件，
    // consigneeInfo--客户信息，pickUpInfo--提货信息，encity--目的地，
    // becity--出发地，verifyInfo--核销信息，boolCollectionInfo--代收款信息，otherInfo--其他信息
    goods() {
      return this.modelShow('goods')
    },
    serviceInfo() {
      return this.modelShow('serviceInfo')
    },
    orderOver() {
      return this.modelShow('orderOver')
    },
    consigneeInfo() {
      return this.modelShow('consigneeInfo')
    },
    pickUpInfo() {
      return this.modelShow('pickUpInfo')
    },
    encity() {
      return this.modelShow('encity')
    },
    becity() {
      return this.modelShow('becity')
    },
    verifyInfo() {
      return this.modelShow('verifyInfo')
    },
    boolCollectionInfo() {
      return this.modelShow('boolCollectionInfo')
    },
    trunk() {
      return this.modelShow('trunk')
    }
  },
  watch: {
    'model.businessNo': {
      handler(val, oldVal) {
        if (!val) {
          return
        }
        getAppointServiceType(val).then(data => {
          this.serviceArr = copy(data, true)
        })
        if (this.model.businessNo === 'BSTE04') {
          this.model.isArriva = 1
        }
        if (this.httpLoading) {
          return
        }
        this.model.serviceNo = []
      },
      immediate: true
    },
    'model.serviceNo': {
      handler(val, oldVal) {
        if (!val.length) {
          return
        }
        this.getConfigure()
      },
      immediate: true
    },
    'model': {
      handler(val, oldVal) {
        console.log(111)
        if (!val) {
          return
        }
        console.log('val', val, oldVal)
        let submitData = copy(val, true)
        // 干线：FHB16
        let trunkNo = submitData.serviceNo.indexOf('FHB16')
        if (Array.isArray(submitData.address) && Array.isArray(submitData.DAddress)) {
          console.log('submitData.address', submitData.address, submitData.DAddress)
          if (submitData.address.length === 3 && submitData.DAddress.length === 3 && parseInt(submitData.allVolume) >= 0 && parseInt(submitData.allWeight) >= 0 && trunkNo >= 0) {
            console.log(33)
            this.getTrunk()
          }
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    range,
    isFormatNumber,
    isInteger,
    requiredIf,
    decimalNumFilter,
    // 添加代收款
    addCollectionBeanList() {
      this.model.collectionBeanList.push({
        costNo: '',
        memo: '',
        money: ''
      })
    },
    handleDeleteCollectionBeanList(index) {
      console.log('this.collectionBeanList.length', this.model.collectionBeanList)
      if (this.model.collectionBeanList.length <= 1) {
        return
      }
      this.model.collectionBeanList.splice(index, 1)
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        } else if (index !== 2) {
          return
        }
        const values = data.map(item => Number(item[column.property]))
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          let num = this.decimalNumFilter(sums[index])
          sums[index] = num
          sums[index] += ' 元'
        } else {
          sums[index] = ''
        }
      })
      return sums
    },
    modelShow(val) {
      let obj = this.configureArr.find(item => item.modelCode === val)
      if (obj) {
        if (Array.isArray(obj.fieldIsHide)) {
          for (const v of obj.fieldIsHide) {
            obj[v] = v // v存在表示该字段不显示
          }
        }
      } else {
        obj = {}
      }
      return obj
    },
    checkBusiness(item) {
      this.model.businessNo = item.value
    },
    checkservice(item) {
      console.log('item', item, this.model.serviceNo)
      if (this.model.serviceNo.indexOf(item.value) >= 0) {
        let index = this.model.serviceNo.indexOf(item.value)
        this.model.serviceNo.splice(index, 1)
      } else {
        this.model.serviceNo.push(item.value)
      }
    },
    getConfigure() {
      let data = {}
      data.businessCode = this.model.businessNo
      if (Array.isArray(this.model.serviceNo)) {
        data.serviceCodes = this.model.serviceNo.join(',')
      } else {
        data.serviceCodes = this.model.serviceNo
      }
      this.$ajax.get('/ms-fahuobao-order/FhbOrder/getModelsByServiceCode', data).then(response => {
        this.configureArr = response.data || []
      })
    },
    uploadImg(item) {
      this.productItem = item
      this.editPicture = true
      this.visibleProduct = true
    },
    bindData() {
      let orderDetail = copy(this.orderDetail, true)
      this.model.businessNo = orderDetail.businessNo
      this.model.serviceNo = orderDetail.serviceNo.split(',') || []
      this.model.orderWay = orderDetail.orderWay
      this.model.wokerUserName = orderDetail.wokerUserName
      this.model.wokerPhone = orderDetail.wokerPhone
      this.model.wokerPickMoney = orderDetail.wokerPickMoney
      this.model.wokerPrice = orderDetail.wokerPrice
      this.model.allVolume = orderDetail.allVolume
      this.model.allWeight = orderDetail.allWeight
      this.model.allPackages = orderDetail.allPackages
      this.model.allCargoPrice = orderDetail.allCargoPrice
      this.model.verfiyType = orderDetail.verfiyType
      this.model.goods = orderDetail.goods
      this.model.goods.forEach(item => {
        let _arr = []
        _arr.push(item.bigClassNo, item.middleClassNo)
        this.$set(item, 'productCodeArr', _arr)
        item.picture = item.picture ? item.picture.split(',') : []
      })
      if (orderDetail.verifyOrderNo) {
        this.model.verifyOrderNoArr = []
      } else {
        this.model.verifyOrderNoArr = [{}]
      }
      orderDetail.verifyOrderNo = orderDetail.verifyOrderNo ? (orderDetail.verifyOrderNo.split(',')) : []
      orderDetail.verifyOrderNo.forEach(item => {
        let obj = {}
        obj.verifyOrderNo = item
        this.model.verifyOrderNoArr.push(obj)
      })
      this.model.collectionBeanList = orderDetail.collectionBeanList || [{
        costNo: '',
        memo: '',
        money: ''
      }]
      this.model.consigneeName = orderDetail.consigneeName
      this.model.consigneePhone = orderDetail.consigneePhone
      this.model.predictServiceDate = orderDetail.predictServiceDate
      this.model.predictServiceDateCode = orderDetail.predictServiceDateCode
      this.model.isElevator = orderDetail.isElevator
      this.model.floor = orderDetail.floor
      if (orderDetail.provinceNo && orderDetail.cityNo && orderDetail.districtNo) {
        this.model.address = [orderDetail.provinceNo, orderDetail.cityNo, orderDetail.districtNo]
      }
      this.model.consigneeAddress = orderDetail.consigneeAddress
      // 提货人信息
      this.model.deliveryName = orderDetail.deliveryName
      this.model.deliveryPhone = orderDetail.deliveryPhone
      this.model.isArriva = orderDetail.isArriva
      this.model.predictDevliveryDate = orderDetail.predictDevliveryDate
      if (orderDetail.deliveryProvinceNo && orderDetail.deliveryCityNo && orderDetail.deliveryDistrictNo) {
        this.model.DAddress = [orderDetail.deliveryProvinceNo, orderDetail.deliveryCityNo, orderDetail.deliveryDistrictNo]
      }
      this.model.deliveryAddress = orderDetail.deliveryAddress
      this.model.deliveryMemo = orderDetail.deliveryMemo
      this.model.memo = orderDetail.memo
      this.model.orderSourceNo = orderDetail.orderSourceNo
      this.model.trunkId = orderDetail.trunkId
      this.model.trunkMoney = orderDetail.trunkMoney
    },
    add() {
      this.model.verifyOrderNoArr.push({})
    },
    del(arr, index) {
      if (arr.length <= 1) {
        return
      }
      arr.splice(index, 1)
    },
    delShopping(arr, index) {
      if (arr.length <= 1) {
        return
      }
      arr.splice(index, 1)
    },
    addcheckShopping(arr) {
      arr.forEach(item => {
        if (!Array.isArray(item.picture)) {
          item.picture = item.picture.split(',')
        }
        if (this.editPicture) {
          this.productItem.picture = item.picture
        } else {
          let obj = {}
          obj.picture = item.picture
          obj.pictureName = item.pictureName
          obj.productCodeArr = item.productCodeArr
          obj.id = item.goodsBaseId
          this.model.goods.push(obj)
        }
      })
    },
    setFileId(fileId) {
      this.newPictureArr.push(fileId)
    },
    getOrderInfo() {
      this.httpLoading = true
      this.$ajax.get('/ms-fahuobao-order/FhbOrderTemp/findFhbOrderTempParticulars', {orderId: this.id}).then(response => {
        if (!response.data) {
          return
        }
        let _orderDetail = copy(response.data, true)
        _orderDetail.goods.forEach(item => {
          this.$set(item, 'productCodeArr', [])
        })
        this.orderDetail = _orderDetail
        this.model.goods = _orderDetail.goods
        this.orderNo = response.orderNo
        this.bindData()
      }).finally(_ => {
        this.httpLoading = false
      })
    },
    submitForm(event, type) {
      this.$refs['ruleForm'].validate((valid) => {
        if (!this.model.businessNo) {
          alert('请选择业务类型！', '提示', 'warning')
          return false
        }
        if (!this.model.serviceNo.length) {
          alert('请选择服务类型！', '提示', 'warning')
          return false
        }
        if (!valid || !this.model.goods.length) {
          alert('请完善信息！', '提示', 'warning')
          return false
        }
        let submitData = copy(this.model, true)
        // 干线：FHB16
        let trunkNo = submitData.serviceNo.indexOf('FHB16')
        if (trunkNo >= 0) {
          let trunkChecked = this.trunkData.find(v => v.checked) || {}
          if (!trunkChecked.id) {
            submitData.trunkId = ''
            submitData.trunkMoney = ''
            alert('请选择干线！', '提示', 'warning')
            return
          }
          submitData.trunkId = trunkChecked.id
          submitData.trunkMoney = trunkChecked.money
        }
        submitData.placeOrderSource = 'web'
        submitData.predictServiceDate = submitData.predictServiceDate ? moment(submitData.predictServiceDate).format('YYYY-MM-DD') : '' // 服务时间
        submitData.provinceNo = dotData(submitData.address, '[0]') || ''
        submitData.cityNo = dotData(submitData.address, '[1]') || ''
        submitData.districtNo = dotData(submitData.address, '[2]') || ''
        submitData.serviceNo = submitData.serviceNo.join(',')
        delete submitData.address // 收件地址
        submitData.predictDevliveryDate = submitData.predictDevliveryDate ? moment(submitData.predictDevliveryDate).format('YYYY-MM-DD') : '' // 提货时间
        console.log('submitData', this.model)
        if ((this.pickUpInfo.modelCode && submitData.isArriva === 1) || (this.becity.modelCode && submitData.isArriva === 1)) {
          submitData.deliveryProvinceNo = dotData(submitData.DAddress, '[0]') || ''
          submitData.deliveryCityNo = dotData(submitData.DAddress, '[1]') || ''
          submitData.deliveryDistrictNo = dotData(submitData.DAddress, '[2]') || ''
        } else {
          // 清楚录单默认提货信息
          submitData.deliveryName = ''
          submitData.deliveryPhone = ''
          submitData.deliveryAddress = ''
          submitData.deliveryMemo = ''
        }
        submitData.verifyOrderNo = []
        submitData.verifyOrderNoArr.forEach(item => {
          submitData.verifyOrderNo.push(item.verifyOrderNo)
        })
        if (!submitData.verfiyType) {
          submitData.verifyOrderNo = ''
        } else {
          submitData.verifyOrderNo = submitData.verifyOrderNo.join(',')
        }
        delete submitData.verifyOrderNoArr
        delete submitData.DAddress // 提货地址
        submitData.goods = []
        submitData.id = this.orderDetail.id
        submitData.orderNo = this.orderDetail.orderNo
        submitData.inputType = this.orderDetail.inputType
        this.model.goods.forEach(item => {
          let obj = {}
          obj.num = item.num
          obj.pictureType = 2
          obj.picture = item.picture.join(',')
          obj.bigClassNo = item.productCodeArr[0]
          obj.middleClassNo = item.productCodeArr[1]
          obj.pictureName = item.pictureName
          obj.goodsId = item.id
          submitData.goods.push(obj)
        })
        if (!dotData(submitData.collectionBeanList, '[0].costNo')) {
          submitData.collectionBeanList = []
        }
        this.sendPost(submitData, event, type)
      })
    },
    sendPost(submitData, event, type) {
      this.loading = true
      let url = ''
      if (type === 'submit') {
        url = '/ms-fahuobao-order/FhbOrder/saveOrder'
      } else {
        url = '/ms-fahuobao-order/FhbOrder/saveOrderTemp'
      }
      this.$ajax.post(url, submitData).then(response => {
        this.loading = false
        if (response.success) {
          this.$confirm('提交成功', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'success'
          }).then(_ => {
            this.$router.push({path: '/ss-fhb/fa-temporary/index'})
          })
        }
      }).finally(_ => {
        this.loading = false
      })
    },
    showModal() {
      this.productItem = ''
      this.editPicture = false
      this.visibleProduct = true
    },
    checkDirectional(val) {
      this.model.orderWay = val
    },
    checkWorker() {
      this.$refs.CheckWorker.show()
    },
    editWorker(data) {
      this.model.wokerUserName = data.workerName
      this.model.wokerPhone = data.workerPhone
    },
    getDeliveryAddress(choice) {
      this.$ajax.get('/ms-fahuobao-user/defaultPickupInfo/get-pickup-info').then(response => {
        if (!response.data) {
          return
        }
        this.deliveryAddress = response.data || []
        if (this.rotueName !== 'faTemporaryInput') { // 只在录单界面默认赋值
          return
        }
        this.deliveryAddress.forEach(item => {
          if (item.isDefault === 1) {
            this.handleDeliveryAddress(item, choice)
          }
        })
      })
    },
    handleDeliveryAddress(item, choice) {
      if (choice) {
        return
      }
      this.$set(this.model, 'deliveryName', item.deliveryName)
      this.$set(this.model, 'deliveryPhone', item.deliveryPhone)
      this.$set(this.model, 'deliveryAddress', item.deliveryAddress)
      let arr = [item.deliveryProvinceNo, item.deliveryCityNo, item.deliveryDistrictNo]
      this.$set(this.model, 'DAddress', arr)
    },
    choiceAddress() {
      this.visibleAddress = true
    },
    getTrunk() {
      let submitData = copy(this.model, true)
      let data = {}
      data.allVolume = submitData.allVolume
      data.allWeight = submitData.allWeight
      data.provinceNo = submitData.address[0]
      data.cityNo = submitData.address[1]
      data.districtNo = submitData.address[2]
      data.deliveryProvinceNo = submitData.DAddress[0]
      data.deliveryCityNo = submitData.DAddress[1]
      data.deliveryDistrictNo = submitData.DAddress[2]
      this.$ajax.table('/ms-warehouse-trunk/line/sorting-trunk', data).then(response => {
        if (response.success) {
          this.totalItem = response.totalItem
          let trunkData = response.data || []
          this.trunkData = trunkData.map(item => {
            if (item.id === this.trunkMap.id) {
              item.checked = true
            } else {
              this.$set(item, 'checked', false)
            }
            return item
          })
        }
        console.log('response', response)
      })
    },
    trunkChange(row) {
      console.log('row', row)
      this.trunkData.map(item => {
        if (item.id === row.id) {
          this.trunkMap = item
          item.checked = true
        } else {
          item.checked = false
        }
      })
    },
    sizeChange(size) {
      this.params.pageSize = size
      this.getTrunk()
    },
    pagination(page) {
      this.params.pageNum = page
      this.getTrunk()
    }
  }
}
// TODO:1034
</script>
<style lang="less" scoped>
  .el-button-margin {
    .el-button {
      margin-left: 0px;
      margin-right: 10px;
    }
  }
</style>
